Imports KanBan.Pgd.Infrastructure.Exceptions
Imports KanBan.Pgd.Presentation.Helpers
Imports KanBan.Pgd.Common
Imports KanBan.Pgd.Domain.Entities
Imports KanBan.Pgd.Infrastructure.Repositories
Imports KanBan.Pgd.Services

Public Class PromoteCandidate

    Public Event CandidatePromoted()

    Private app As New PgdApp()

    ''' <summary>
    ''' Property accesor to the shared Search Request.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property SearchRequest() As SearchRequest
        Get
            Return EditSearchRequestContainer.SearchRequest
        End Get
        Set(ByVal value As SearchRequest)
            EditSearchRequestContainer.SearchRequest = value
        End Set
    End Property

    Public Sub New(ByVal candidateId As Guid)

        Try

            ' This call is required by the Windows Form Designer.
            InitializeComponent()

            ' Add any initialization after the InitializeComponent() call.

            Me.CandidateId = candidateId

            Dim candidate = app.Candidates.GetById(candidateId)

            Me.FormTip = "Are you sure you want to promote this Candidate? By doing this you are finishing the Search Request"
            Me.CandidateName = candidate.Name
            Me.Profile = Me.SearchRequest.SearchedProfile.Name

        Catch ex As Exception

            FormHelper.ShowError(ExceptionHandler.HandleException(ex))

        End Try

    End Sub

#Region "Property Binding"

    Public Property FormTip() As String
        Get
            Return txtFormTip.Text
        End Get
        Set(ByVal value As String)
            txtFormTip.Text = value
        End Set
    End Property

    Private _candidateId As Guid
    Public Property CandidateId() As Guid
        Get
            Return _candidateId
        End Get
        Set(ByVal value As Guid)
            _candidateId = value
        End Set
    End Property

    Public Property CandidateName() As String
        Get
            Return lblNameText.Text
        End Get
        Set(ByVal value As String)
            lblNameText.Text = value
        End Set
    End Property

    Public Property Profile() As String
        Get
            Return lblProfileText.Text
        End Get
        Set(ByVal value As String)
            lblProfileText.Text = value
        End Set
    End Property

#End Region

#Region "Event Handling"

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

    Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click

        Try

            Me.PromoteCandidate()

        Catch ex As Exception

            FormHelper.ShowError(ExceptionHandler.HandleException(ex))

        End Try

    End Sub

#End Region

#Region "Private Members"


    Private Sub PromoteCandidate()

        Dim candidate = app.Candidates.GetById(Me.CandidateId)

        'Delete all interviews where this Candidate has participated except the ones in this Search Request.
        For Each i As Interview In candidate.Interviews
            Dim interviewId = i.Id
            If Not Me.SearchRequest.Interviews.Any(Function(x) x.Id = interviewId) Then
                app.SearchRequests.DeleteInterview(interviewId)
            End If
        Next

        'Delete the Candidate from all Search Requests where he participated except this one.
        For Each sr As SearchRequest In candidate.SearchRequests
            If sr.Id <> Me.SearchRequest.Id Then
                sr.Candidates.Remove(candidate)
                app.SearchRequests.SaveOrUpdate(sr)
            End If
        Next

        Dim profile = Me.SearchRequest.SearchedProfile
        Dim userId = Guid.NewGuid()

        'Create the new User based on the Candidate.
        Dim user As New User(userId, _
                             candidate.Name, _
                             Me.CreateUserName(candidate.Name), _
                             PasswordGenerator.GeneratePassword(), _
                             profile)

        app.Users.SaveOrUpdate(user)

        candidate.IsPromoted = True
        app.Candidates.SaveOrUpdate(candidate)

        Me.SearchRequest.SetStatus(Domain.SearchRequestStatus.Finished)

        RaiseEvent CandidatePromoted()

        Me.Close()

    End Sub

    Private Function CreateUserName(ByVal fullName As String) As String

        Return fullName.Replace(" ", "").ToLower()

    End Function

#End Region

End Class